﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenSource.UPnP;
using System.Xml;
using System.Windows.Markup;
using System.IO;
using System.Reflection;
using System.Windows.Threading;
using System.Runtime.CompilerServices;
using System.Collections;
using System.Diagnostics;



namespace DispositifAffichage1
{

    public class ServeurDispositifAffichage
    {
        private UPnPDevice root;
        private UPnPService list;
        ArrayList parametres = new ArrayList();
        ArrayList surfaceparametres = new ArrayList();

        MainWindow mainwindow;
        private Boolean fileReçu = false;


        string description;
        string nomSender;

        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        Dictionary<string, System.Drawing.Rectangle> dictionaryFinal = new Dictionary<string, System.Drawing.Rectangle>();
        Communication2 communication2;

        //ClientDispositifAffichage1 cl;
        public static int cmp = 0;

        /**
            * \ServeurDispositifAffichage      le constructeur du classe 'ServeurDispositifAffichage'

       
        
        public ServeurDispositifAffichage(Dictionary<string, System.Drawing.Rectangle> dictionaryFinal_2)
        {
            // TODO: Complete member initialization
            this.dictionaryFinal = dictionaryFinal_2;
        }
 */
        public ServeurDispositifAffichage()
        {
            communication2 = new Communication2(this);
        }

        public ServeurDispositifAffichage(MainWindow mainw)
        {
            mainwindow = mainw;
            communication2 = new Communication2(this);
            string s = "";
            root = UPnPDevice.CreateRootDevice(360, 1.0, "");
            root.FriendlyName = "DispositifAffichage1";
            root.DeviceURN = "urn:schemas-upnp-org:device:DispositifAffichage1:1";
            root.HasPresentation = false;
            root.Manufacturer = "sbai";
            root.ModelName = "UPnP sample for kids";
            root.UniqueDeviceName = "12345678-153f-430d-82f3-dff320c17111";

            list = new UPnPService(1.0, "Sample", "urn:schemas-upnp-org:service:Sample:1", true, this);
            list.AddStateVariable(new UPnPStateVariable("Text", s));


            list.AddMethod("loadFile");
            list.AddMethod("recupereParametreInterface");
            list.AddMethod("updateresultatFinal");
            list.AddMethod("getRectsFinal");
            root.AddService(list);
            root.Advertise();


        }



        /**
        * \startUPnP       pour démarrer le device

        */
        public void startUPnP()
        {
            root.StartDevice();
            Console.WriteLine("Serveur start");
        }

        /**
        * \stopUPnP       pour arrêter le device
        */
        public void stopUPnP()
        {
            root.StopDevice();
            Console.WriteLine("Serveur stopped");

        }


        /**
        * \loadFile       pour télécharger le fichier de description
        * \param         InFile est le fichier de descrption passé sous forme d'une chaîne de caractères
        */
        public void loadFile(string InFile, string name, string IdReservation)
        {
            description = InFile;
            nomSender = name;
            Console.WriteLine("IdReservation = " + IdReservation);


            if (dictionaryFinal.ContainsKey(IdReservation))
            {

                Console.WriteLine("il y a une réservation pour cette interface");

                cmp = 1;

                if (InFile == "")
                {

                    Console.WriteLine("fichier de description reçu est vide");
                }
                else
                {
                    fileReçu = true;

                    ///commande=namespace qui existe dans le fichier de description xaml
                    string commande = InFile.Substring(InFile.IndexOf("x:Class"), InFile.IndexOf("  "));

                    Console.WriteLine("la commande à afficher est: \n" + commande);

                    ///j'ai supprimé le namespace
                    InFile = InFile.Replace(commande, "");

                    // mainwindow.chargement_UI(InFile);


                    mainwindow.createinterface(InFile, name, IdReservation, dictionaryFinal);
                }

            }
            else { Console.WriteLine("il n'y a pas de réservation pour cette interface"); }




        }


        /**
        * \recupereParametreInterface       pour récupèrer les paramètres (widht, height)des interfaces q'un client veut afficher (côté application Communicante)
        * \param         s4 est le widht
         * \param        s5 est le height
        */
        public string recupereParametreInterface(string name, int width, int height)
        {

            // Le code dans le bloc suivant sera protégé
            lock (this)
            {
                string resultat = "";
                //Console.WriteLine(width);
                //Console.WriteLine(height);
                //Console.WriteLine(name);
                resultat = communication2.compare(name, width, height);
                Console.WriteLine("");
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine(width);
                Console.WriteLine(height);
                Console.WriteLine(resultat);
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
                Console.WriteLine("");

                if (resultat.Contains("Oui"))
                {

                    Console.WriteLine("il faut initialiser le timer");

                    //Démarrage du Timer 

                    mainwindow.decTimer();

                }

                return resultat;


            }


        }





        public void updateresultatFinal(string decisionFinale)
        {
            // Le code dans le bloc suivant sera protégé
            lock (this)
            {
                Console.WriteLine("valFinal = " + decisionFinale);

                if (decisionFinale.Contains("Non"))
                {
                    communication2.updateResultatFinal();
                }
            }
        }


        public void annulerReservation()
        {

            Console.WriteLine("Annulation de la réservation, temps d'attente est terminé");
            communication2.updateResultatFinal2();

        }

        public void getRectsFinal(System.Drawing.Rectangle[] res)
        {

            Console.WriteLine("méthode getRectsFinal est invoquée !!!!");

            Console.WriteLine("la longueur du res est: " + res.Length);

            if (!fileReçu)
            {
                Console.WriteLine("Annulation de la réservation");
            }
            else
            {
                Console.WriteLine("Allez commencer la génération de l'interface graphique !!!!!!!!!!!!");
                Console.WriteLine("le nom est : " + nomSender);
                Console.WriteLine("file received: \n" + description);


                //mainwindow.createinterface(description, nomSender,"");
            }

        }


        public void getdictionaryFinal(Dictionary<string, System.Drawing.Rectangle> dictionaryFinal_2)
        {

            dictionaryFinal = dictionaryFinal_2;

            Console.WriteLine("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

            foreach (KeyValuePair<string, System.Drawing.Rectangle> kvp in dictionaryFinal)
            {
                Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
            }
            Console.WriteLine("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");


        }

    }
}
